#include <DrawingMap.hpp>

Drawing::Map::~Map()
{}

Drawing::Map::Map()
{}

int Drawing::Map::getId()
{
	return this->id;
}

void Drawing::Map::setId(int id)
{
	this->id = id;
}

void Drawing::Map::setFileName(std::string filename)
{
	Modeling::Map::setFileName(filename);
	//int id = Drawing::Map::loadBitmap(filename);
	this->setId(id);
}

int Drawing::Map::num_texture = -1;

int Drawing::Map::loadBitmap(std::string filename) throw (Exceptions::FileNotFoundException)
{
	// these are types to work with the Bitmap files
	BMPFILEHEADER fileheader;
	BMPINFOHEADER infoheader;
	TRGBTRIPLE rgb;

	FILE* mapFile;// (MapFileName.c_str(), fstream::in | fstream::out);

	//if ( !mapFile.is_open() )
	if( (mapFile = fopen(filename.c_str(), "rb")) == NULL)
		throw Exceptions::FileNotFoundException( filename + std::string(" no such file or directory") );
    
	Drawing::Map::num_texture++; // The counter of the current texture is increased

	//mapFile.read( (char*)&fileheader, sizeof(BMPFILEHEADER) );
	//mapFile.read( (char*)&infoheader, sizeof(BMPINFOHEADER) );
	fread(&fileheader, sizeof(BMPFILEHEADER), 1, mapFile); // Read the fileheader
    
	fseek(mapFile, sizeof(BMPFILEHEADER), SEEK_SET); // Jump the fileheader
	fread(&infoheader, sizeof(BMPINFOHEADER), 1, mapFile); // and read the infoheader

	//The pointer to the memory zone in which we will load the texture
 	unsigned char* l_texture = new unsigned char[infoheader.biWidth * infoheader.biHeight * 4]; 

	int j=0;
	// At this point we can read every pixel of the image
	for (int i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
	{            
            // We load an RGB value from the file
			//mapFile.read( (char*)&rgb, sizeof(TRGBTRIPLE) );
            fread(&rgb, sizeof(rgb), 1, mapFile);
            
            // And store it
            l_texture[j+0] = rgb.rgbtRed; // Red component
            l_texture[j+1] = rgb.rgbtGreen; // Green component
            l_texture[j+2] = rgb.rgbtBlue; // Blue component
            l_texture[j+3] = 255; // Alpha value
            j += 4; // Go to the next position
	}

	//mapFile.close();
	fclose(mapFile); // Closes the file stream

	glBindTexture(GL_TEXTURE_2D, Drawing::Map::num_texture); // Bind the ID texture specified by the 2nd parameter

	// The next commands sets the texture parameters
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // We combine the color with the original surface color.

	// Finally we define the 2d texture
	glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

	// And create 2d mipmaps for the minifying function
	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

	delete[] l_texture;

	return (Drawing::Map::num_texture); // Returns the current texture OpenGL ID
}
